package com.example.backend2.inteceptor;

import com.example.backend2.context.LoginMemberContext;
import com.example.backend2.domain.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MemberInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse response, Object handler) throws Exception {
        //获取header的token参数
        Object loginMember = req.getSession().getAttribute("loginMember");
        if (loginMember != null) {
            LoginMemberContext.setMember((User) loginMember);
        }
        return true;
    }

}
